

/**

 * Created by CJB on 2017-01-03 10:52:25.
 */
var loadDiv = null;
$(function () {
	loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
    initAddWindow();
    //去除所有控件的虚线框
	removeBlur();
});

/**
 * 初始化添加窗口
 */
function initAddWindow(){
	var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
    zTree = zTree.setting.callback.onClick = onDialogTreeOnClick("dictionaryAddList","parentId");
	var toolTips = new Object();
    //设置表单验证
    var validator = $("#addForm").validate({
    	rules:{
    		code:{
    			required:true,
        		remote:{
        			url: getPath() +  "app/dictionary/validateCode",
        			type: "post",
        			data:{
        				newCode:function(){
    					return $("#label").val();
    					}
        			}
        		}
    		},
            name:"required",
           
            sortFlag:{
            	required:true,
	            digits:true
        	},
        	dictionaryAddList:"required"
        },
        messages:{
        	
            code:{required:"请输入字典编码！",
        		remote: "已存在此编码，请重新输入！"},
        	name:"请输入字典名称",
        	
        	sortFlag:{required:"请输入排序",digits:"只能输入数字"},
            authorityAddList:"请选择父节点"
        },
        errorPlacement: function(eror,element){       	
            var toolTip = createTooltip(toolTips,element.attr("id"));
            toolTip.setContent(eror.text());
            toolTip.close();
            element.addClass("error-border");

        },
        success:function(label){
            var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
            var toolTip = toolTips[elementId];
            $("#" + elementId).removeClass("error-border");
            toolTip.close();
            toolTip.detach();
        },
        submitHandler:function(form){
        	loadDiv.showLoading();
        	post(getPath() + "app/dictionary/add",
        			$("#addForm").serialize(),
                    function(resultObject){
        		loadDiv.hideLoading();
        		var errors = resultObject.errors;
                //判断后台是否有返回错误消息
                if(errors && errors.length > 0){
                    for(var i=0; i < errors.length; i++){
                        var error = errors[i];
                        var toolTip = createTooltip(toolTips,error.field);
                        toolTip.setContent(error.defaultMessage);
                        $("#" + error.field).addClass("error-border");
                    }
                }else{
                var item = resultObject.item;
                var parentNode = getNodeById('parentMenuTree', item.parentId);
                var menuparentNode = getNodeById('menuTree', item.parentId);
                if(parentNode){
                    var newNode = {};
                    newNode.id = item.id;
                    newNode.pId = item.parentId;
                    newNode.name = item.name;
                    newNode.title = item.name;
                    var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
                    zTree.addNodes(parentNode, newNode);
                    var zTree = $.fn.zTree.getZTreeObj('menuTree');
                    zTree.addNodes(menuparentNode, newNode);
                    zTree.expandNode(menuparentNode,true);
                    createWindow("添加成功");
                    $(".grid-list").load(getPath() + "app/dictionary/add/" + 0);
                	//createWindow("添加成功",{url: getPath() + "app/dictionary/list"}).dialog("open");
                }
            }
        	},function(resultObject){
            	loadDiv.hideLoading();
        		createWindow(resultObject).dialog('open');
            })
    }
});
/*  //隐藏列表，显示添加
	$(".grid-list").show();
	$('.grid-list').hide();
	//取消
	$('#cancleBtn').click(function(){
		$(".grid-list").hide();
		$('.grid-list').show();
	});*/
    $("#dictionaryAddList").click({menuContentId:"parentMenu"}, showMenu);
}

$("#refreshCache").click(function(){
	 createWindow('确定要刷新缓存？',{
        cancleEnable:true,
        callBack:function(){
        	$("#loadDiv").loading().showLoading();
        	 post(getPath() + "app/dictionary/refreshCache",{},function(result){
        		$("#loadDiv").loading().hideLoading();
   			 createWindow("缓存刷新成功").dialog("open");
   		 });
        }
    }).dialog('open');
});


function heavyLoad(){
	$(".grid-list").load(getPath() + "app/dictionary/add/"+0);
}